package conectar;

import java.sql.*;
import java.io.Serializable;

public class Conectar implements Serializable {

    public Connection miconexion = null;//private Connection con= null;
    public Statement stSentencias = null;//Statement st=null;
    public ResultSet rsDatos = null; //ResultSet res=null;
    public PreparedStatement psPrepararSentencia = null;
    
    String driver="com.mysql.jdbc.Driver";
    String url="jdbc:mysql://localhost:3306/votoelectronico";
    String userDB="root";
    String passDB="";
    

    public Conectar() throws SQLException, ClassNotFoundException {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/votoelectronico";

            miconexion = DriverManager.getConnection(url, "root", "");

            stSentencias = miconexion.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

        } catch (ClassCastException ex) {
            throw ex;
        } catch (SQLException ex1) {
            throw ex1;
        } 

    }

    public ResultSet consulta(String sql) throws SQLException {
        try {
            //Solo para hacer consultas
            rsDatos = stSentencias.executeQuery(sql);
        } catch (SQLException ex) {
            throw ex;
        }

        return rsDatos;
    }

    //realizar inserciones y eliminaciones
    public void ejecutar(String sql) throws SQLException {
        try {
            stSentencias.execute(sql);
        } catch (SQLException ex) {
            throw ex;
        }
    }
    
    public void actualizar(String sql) throws SQLException{
        try{
            stSentencias.executeUpdate(sql);
        }catch(SQLException ex){
            throw ex;
        }
    }
    
    public void desconectar(){
        try{ 
            miconexion.close();
            stSentencias.close();
            rsDatos.close();
        } 
        catch(Exception ex){ 
            System.out.println("No se cerró la conexion existente a MySQL"); 
            ex.printStackTrace();
            ex.getMessage();
        }
    }
}
